package com.ws.shop.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ws.shop.bean.HtmlConfig;
import com.ws.shop.bean.Page;
import com.ws.shop.dao.ArticleDao;
import com.ws.shop.entity.Article;
import com.ws.shop.service.ArticleService;
import com.ws.shop.service.HtmlService;
import com.ws.shop.util.CommonUtil;
import com.ws.shop.util.StringUtil;
import com.ws.shop.util.TemplateConfigUtil;

@Service(value="articleService")
public class ArticleServiceImpl implements ArticleService {
	@Resource(name="articleDao")
	private ArticleDao articleDao;
	@Resource(name="htmlService")
	private HtmlService htmlService;

	@Cacheable(value="articleColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page findArticleListPage(long pageno,int pagesize, String title) {
		return articleDao.findArticleListPage(pageno,pagesize,title);
	}

	@CacheEvict(value="articleColl",allEntries=true)
	@Transactional(rollbackFor=Exception.class)
	public void deleteArticleById(int id) {
		articleDao.deleteArticleById(id);
	}

	@CacheEvict(value="articleColl",allEntries=true)
	@Transactional(rollbackFor=Exception.class)
	public void deleteArticleByIds(int[] ids) {
		articleDao.deleteArticleByIds(ids);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Article findArticleById(int id) {
		return articleDao.findArticleById(id);
	}

	@CacheEvict(value="articleColl",allEntries=true)
	@Transactional(rollbackFor=Exception.class)
	public void saveOrUpdateArticle(Article article) {
		if(article.getId() == 0 || StringUtil.isEmpty(article.getLink())){
			HtmlConfig htmlConfig = TemplateConfigUtil.getHtmlConfig(HtmlConfig.ARTICLE_CONTENT);
			String htmlFilePath = htmlConfig.getHtmlFilePath(CommonUtil.getRandomString(8));
			article.setLink(htmlFilePath);
		}
		articleDao.saveOrUpdateArticle(article);
		if(article.getState() == 1){
			htmlService.articleContentBuildHtml(article);
		}
	}

	@Cacheable(value="articleColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Article> findArticlesByCategery(){
		return articleDao.findArticlesByCategery();
	}
	
	@Cacheable(value="articleColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Map<String,Object>> getArticle(int state ,int cid,int limit){
		return articleDao.getArticle(state, cid, limit);
	}

	public void getArticlesByCid(Page page, int cid,int state) {
		articleDao.getArticlesByCid(page, cid,state);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public List<Map<String,Object>> getArticlesByCondition(long id,int cid,int type){
		return articleDao.getArticlesByCondition(id, cid, type);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page findArticlesByCids(int pageno, int pagesize, int[] cids,
			String start, String end) {
		return articleDao.findArticlesByCids(pageno, pagesize, cids, start, end);
	}

	@Cacheable(value="articleColl")
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	public Page findArticleListPage(int pageno, int pagesize, int cid) {
		return articleDao.findArticleListPage(pageno,pagesize,cid);
	}
	
}
